package com.icetec.iceintel.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.framework.IceBaseFragment;
import com.icetec.iceintel.framework.dragGridView.DragGridView;
import com.icetec.iceintel.framework.dragGridView.MoveCallback;
import com.icetec.iceintel.framework.dynamicgrid.DynamicGridView;

import com.icetec.iceintel.ui.activity.AlarmListActivity;
import com.icetec.iceintel.ui.activity.SchemeSelectActivity;
import com.icetec.iceintel.utility.DecodingBimapToBlur;
import com.icetec.iceintel.utility.DecodingBimapToBlurCallback;
import com.icetec.R;

import com.icetec.iceintel.ui.adapter.SchemeAdapter;
import com.icetec.iceintel.ui.activity.MainActivity;
import com.icetec.iceintel.netrequest.InstrutionSender;
import com.icetec.iceintel.framework.ThreadExecutor;
import com.icetec.iceintel.utility.ToastUtility;

/**
 * Created by amo on 2014/12/20.
 * 灯具模式控制界面，用以给用户自定义快速的模式
 */
public class SchemeFragment  extends IceBaseFragment {

    SchemeAdapter schemeAdapter;

    //DynamicGridView lightList;
    //DynamicGridView smallLightList;
    DragGridView schemeList;
    DragGridView smallSchemeList;
    Button toSmallbutton;
    Button toSmallbuttonLeft;

    TextView modifyTv;
    TextView groupTv;
    TextView alarmTv;
    TextView backTv;

    ImageView backGroundBurnd;
    ImageView page_up_arrow;

    private RelativeLayout alarm_rely;

    boolean extendlock;
    boolean isModifying;

    AnalogClock analogClock;

    static  boolean isAllbtnActive=false;

    InstrutionSender instrutionSender;


    public  boolean oreation_left=true;
    public  boolean oreation_right=false;
    public   boolean isSmallPanelShowing=false;
    public   boolean smallOreation=oreation_right;



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View parent = inflater.inflate(R.layout.page_scheme, container,
                false);
        schemeList =  ((DragGridView)parent.findViewById(R.id.light_list_gridView));
        smallSchemeList=((DragGridView)parent.findViewById(R.id.light_list_gridView_small));



        toSmallbutton=(Button)parent.findViewById(R.id.btn_tosmall);
        toSmallbuttonLeft=(Button)parent.findViewById(R.id.btn_tosmall_left);

        backGroundBurnd=(ImageView)parent.findViewById(R.id.lightlist_background_burd);
        page_up_arrow=(ImageView)parent.findViewById(R.id.lightlist_pageuparrow);

        modifyTv=(TextView)parent.findViewById(R.id.lightlist_panel_edit);
        groupTv=(TextView)parent.findViewById(R.id.lightlist_panel_groupset);
        alarmTv=(TextView)parent.findViewById(R.id.page_scheme_alarmsetting_tv);
        backTv = (TextView)parent.findViewById(R.id.lightlist_panel_back);

        analogClock=(AnalogClock)parent.findViewById(R.id.page_scheme_analogClock);

        alarm_rely = (RelativeLayout) parent.findViewById(R.id.scheme_alarm_ly);

        schemeAdapter=new SchemeAdapter();
        schemeList.setAdapter(schemeAdapter);
        smallSchemeList.setAdapter(schemeAdapter);


        instrutionSender=new InstrutionSender();
        isModifying=false;
        extendlock=false;


        alarmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.shareInstance, AlarmListActivity.class);
                startActivity(intent);
            }
        });


        analogClock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.shareInstance, AlarmListActivity.class);
                startActivity(intent);
            }
        });

        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                schemeAdapter.setModifyMode(false);
                schemeAdapter.updateListViewFromDb();
                modifyTv.setText(R.string.modify);
                groupTv.setVisibility(View.GONE);
                backTv.setVisibility(View.GONE);
                alarm_rely.setVisibility(View.VISIBLE);
                isModifying=false;
                MainActivity.shareInstance.setScrollEnable(true);
                MainActivity.shareInstance.setToSmallRightButtonVisibility(true);
                MainActivity.shareInstance.setToSmallLeftButtonVisibility(true);
                MainActivity.shareInstance.setIndicatorLinerlayoutVisibility(true);
                page_up_arrow.setVisibility(View.VISIBLE);
            }
        });

        schemeList.setMoveCallback(new MoveCallback() {
            @Override
            public void startMove() {
                MainActivity.shareInstance.setScrollEnable(false);
            }

            @Override
            public void stopMove() {
                MainActivity.shareInstance.setScrollEnable(true);
                schemeAdapter.saveAllDeviceReArrange();
                schemeAdapter.saveAllDeviceReArrange();
                schemeAdapter.updateListViewFromDb();
            }
        });

        smallSchemeList.setMoveCallback(new MoveCallback() {
            @Override
            public void startMove() {
                MainActivity.shareInstance.setScrollEnable(false);
            }

            @Override
            public void stopMove() {
                MainActivity.shareInstance.setScrollEnable(true);
                schemeAdapter.saveAllDeviceReArrange();
                schemeAdapter.updateListViewFromDb();
            }
        });
      /*  toSmallbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isShowingSmallRight&&!extendlock){

                    isShowingSmallRight=false;
                    smallLightList.setVisibility(View.GONE);
                    smallLightList.startAnimation(animationout);



                    backGroundBurnd.setVisibility(View.GONE);
                    backGroundBurnd.startAnimation(animationout);


                    toSmallbutton.setBackgroundResource(R.drawable.tosmall);
                    toSmallbuttonLeft.setBackgroundResource(R.drawable.change_leftright_icon);

                    schemeAdapter.updateListViewFromDb();
                    lightList.setVisibility(View.VISIBLE);


                }

                else if(isShowingSmallLeft&&!extendlock){

                    whichOreation="right";

                    toSmallbuttonLeft.setBackgroundResource(R.drawable.change_leftright_icon);
                    toSmallbutton.setBackgroundResource(R.drawable.tobig);

                    smallLightList.setVisibility(View.GONE);
                    smallLightList.startAnimation(animationout);

                    RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallLightList.getLayoutParams();
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    //      layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);

                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);

                    smallLightList.setVisibility(View.VISIBLE);
                    smallLightList.startAnimation(animationin);

                    isShowingSmallLeft=false;
                    isShowingSmallRight=true;


                }


                else if(!extendlock&&whichOreation.equals("right")){

                    RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallLightList.getLayoutParams();
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    //        layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
                    smallLightList.setLayoutParams(layoutParams);


                    schemeAdapter.updateListViewFromDb();
                    final View view = MainActivity.shareInstance.getWindow().getDecorView();
                    view.setDrawingCacheEnabled(true);
                    view.buildDrawingCache();
                    DecodingBimapToBlur decodingBimapToBlur=new DecodingBimapToBlur();

                    decodingBimapToBlur.setDecodingBimapToBlurCallback(new DecodingBimapToBlurCallback() {
                        @Override
                        public void onStart() {
                            extendlock=true;
                        }

                        @Override
                        public void onPost(Bitmap bitmap) {
                            backGroundBurnd.setImageBitmap(bitmap);
                            Log.i("模糊回调", "成功模糊化");
                            view.destroyDrawingCache();
                            backGroundBurnd.setVisibility(View.VISIBLE);
                            backGroundBurnd.startAnimation(animationin);
                            lightList.setVisibility(View.GONE);
                            lightList.startAnimation(animationout);
                            extendlock=false;
                        }
                    });

                    decodingBimapToBlur.executeOnExecutor(ThreadExecutor.executorService,view.getDrawingCache());


                    isShowingSmallRight=true;

                    smallLightList.setVisibility(View.VISIBLE);
                    smallLightList.startAnimation(animationTrans);


                    toSmallbutton.setBackgroundResource(R.drawable.tobig);
                    toSmallbuttonLeft.setBackgroundResource(R.drawable.change_leftright_icon);


                }

                else if(whichOreation.equals("left")){

                    toSmallbuttonLeft.setBackgroundResource(R.drawable.change_leftright_icon);
                    toSmallbutton.setBackgroundResource(R.drawable.tosmall);
                    whichOreation="right";


                }


            }
        });







  /*   toSmallbuttonLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isShowingSmallLeft&&!extendlock){

                    isShowingSmallLeft=false;
                    smallLightList.setVisibility(View.GONE);
                    smallLightList.startAnimation(animationout);



                    backGroundBurnd.setVisibility(View.GONE);
                    backGroundBurnd.startAnimation(animationout);


                    toSmallbuttonLeft.setBackgroundResource(R.drawable.tosmallleft);
                    toSmallbutton.setBackgroundResource(R.drawable.change_leftright_icon);

                    schemeAdapter.updateListViewFromDb();
                    lightList.setVisibility(View.VISIBLE);


                }

                else if(isShowingSmallRight&&!extendlock){

                    toSmallbutton.setBackgroundResource(R.drawable.change_leftright_icon);

                    toSmallbuttonLeft.setBackgroundResource(R.drawable.tobigleft);

                    smallLightList.setVisibility(View.GONE);
                    smallLightList.startAnimation(animationout);

                    RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallLightList.getLayoutParams();
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);


                    smallLightList.setVisibility(View.VISIBLE);
                    smallLightList.startAnimation(animationin);

                    isShowingSmallRight=false;
                    isShowingSmallLeft=true;


                }


                else if(!extendlock&&whichOreation.equals("left")){

                    RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallLightList.getLayoutParams();
                    //     layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    smallLightList.setLayoutParams(layoutParams);


                    schemeAdapter.updateListViewFromDb();
                    final View view = MainActivity.shareInstance.getWindow().getDecorView();
                    view.setDrawingCacheEnabled(true);
                    view.buildDrawingCache();
                    DecodingBimapToBlur decodingBimapToBlur=new DecodingBimapToBlur();

                    decodingBimapToBlur.setDecodingBimapToBlurCallback(new DecodingBimapToBlurCallback() {
                        @Override
                        public void onStart() {
                            extendlock=true;

                        }

                        @Override
                        public void onPost(Bitmap bitmap) {
                            backGroundBurnd.setImageBitmap(bitmap);
                            Log.i("模糊回调","成功模糊化");
                            view.destroyDrawingCache();
                            backGroundBurnd.setVisibility(View.VISIBLE);
                            backGroundBurnd.startAnimation(animationin);
                            lightList.setVisibility(View.GONE);
                            lightList.startAnimation(animationout);
                            extendlock=false;
                        }
                    });

                    decodingBimapToBlur.executeOnExecutor(ThreadExecutor.executorService,view.getDrawingCache());

                    //   lightList.setVisibility(View.GONE);

                    isShowingSmallLeft=true;

                    smallLightList.setVisibility(View.VISIBLE);
                    smallLightList.startAnimation(animationTrans);


                    toSmallbuttonLeft.setBackgroundResource(R.drawable.tobigleft);
                    toSmallbutton.setBackgroundResource(R.drawable.change_leftright_icon);



                }

                else if(whichOreation.equals("right")){

                    toSmallbutton.setBackgroundResource(R.drawable.change_leftright_icon);
                    toSmallbuttonLeft.setBackgroundResource(R.drawable.tosmallleft);
                    whichOreation="left";


                }
            }
        });*/



        return parent;

    }







    @Override
    public void onStart() {
        super.onStart();



        modifyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isModifying){

                    isModifying = false;
                    schemeList.enableMove();
                    modifyTv.setText(R.string.modify);
                    schemeAdapter.setModifyMode(false);
                    schemeAdapter.updateListViewFromDb();
                    groupTv.setVisibility(View.GONE);
                    backTv.setVisibility(View.GONE);
                    alarm_rely.setVisibility(View.VISIBLE);
                    MainActivity.shareInstance.setScrollEnable(true);
                    MainActivity.shareInstance.setIndicatorLinerlayoutVisibility(true);
                    MainActivity.shareInstance.setToSmallLeftButtonVisibility(true);
                    MainActivity.shareInstance.setToSmallRightButtonVisibility(true);
                    page_up_arrow.setVisibility(View.VISIBLE);


                }


                else{

                    modifyTv.setText(R.string.finish);
                    isModifying = true;
                    schemeList.disEnableMove();
                    schemeAdapter.setModifyMode(true);
                    schemeAdapter.updateListViewFromDb();
                    alarm_rely.setVisibility(View.GONE);
                    groupTv.setVisibility(View.VISIBLE);
                    backTv.setVisibility(View.VISIBLE);
                    MainActivity.shareInstance.setScrollEnable(false);
                    MainActivity.shareInstance.setIndicatorLinerlayoutVisibility(false);
                    MainActivity.shareInstance.setToSmallLeftButtonVisibility(false);
                    MainActivity.shareInstance.setToSmallRightButtonVisibility(false);
                    page_up_arrow.setVisibility(View.GONE);

                }
            }
        });



        groupTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(schemeAdapter.getCount() >= 9){
                    ToastUtility.show(R.string.limit_scheme);
                }else {
                    Intent intent = new Intent(MainActivity.shareInstance, SchemeSelectActivity.class);
                    startActivity(intent);

                    MainActivity.shareInstance.setScrollEnable(true);
                    MainActivity.shareInstance.setIndicatorLinerlayoutVisibility(true);
                    MainActivity.shareInstance.setToSmallLeftButtonVisibility(true);
                    MainActivity.shareInstance.setToSmallRightButtonVisibility(true);
                    schemeAdapter.setModifyMode(false);
                    modifyTv.setText(R.string.modify);
                    groupTv.setVisibility(View.GONE);
                    backTv.setVisibility(View.GONE);
                    page_up_arrow.setVisibility(View.VISIBLE);
                    alarm_rely.setVisibility(View.VISIBLE);
                    isModifying = false;
                }
            }
        });
    }

    @Override
    public void onResume() {
        Log.i("schemeFragme", "onResume");
        super.onResume();
        schemeAdapter.updateListViewFromDb();
        refreshUI();

    }

    public void refreshUI() {



        Log.i("准备刷新UI", "刷新");
        if(extendlock)return;

        //左缩小面版正展开
        if(AppApplication.smallOreation==AppApplication.oreation_left
                &&AppApplication.isSmallPanelShowing){

            if(!isSmallPanelShowing){

                //未展开，则人为展开
                isSmallPanelShowing=true;
                smallOreation=oreation_left;

                RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallSchemeList.getLayoutParams();
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);




                final View view = MainActivity.shareInstance.getWindow().getDecorView();
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                DecodingBimapToBlur decodingBimapToBlur=new DecodingBimapToBlur();

                decodingBimapToBlur.setDecodingBimapToBlurCallback(new DecodingBimapToBlurCallback() {
                    @Override
                    public void onStart() {
                        extendlock=true;

                    }

                    @Override
                    public void onPost(Bitmap bitmap) {

                        Log.i("模糊回调","成功模糊化");

                        backGroundBurnd.setImageBitmap(bitmap);

                        view.destroyDrawingCache();

                       presentSmallList();
                       presentBurdBg();
                       hideList();

                        extendlock=false;
                    }
                });

                decodingBimapToBlur.executeOnExecutor(ThreadExecutor.executorService,view.getDrawingCache());
            }


            //面板已经展开但是在右边
            else if (smallOreation==oreation_right){


                smallOreation=oreation_left;

                hideSmallList();

                RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallSchemeList.getLayoutParams();
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

                presentSmallList();

            }




        }


        //左缩小面板不展开
        else if(AppApplication.smallOreation==AppApplication.oreation_left
                &&!AppApplication.isSmallPanelShowing){


            if(isSmallPanelShowing){

               hideBurdBg();
               hideSmallList();
               presentList();


            }




        }


        //右缩小面板已展开
        else if(AppApplication.smallOreation==AppApplication.oreation_right
                &&AppApplication.isSmallPanelShowing){

            if(!isSmallPanelShowing){

                //未展开，则人为展开
                isSmallPanelShowing=true;
                smallOreation=oreation_right;

                RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallSchemeList.getLayoutParams();
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);

                final View view = MainActivity.shareInstance.getWindow().getDecorView();
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                DecodingBimapToBlur decodingBimapToBlur=new DecodingBimapToBlur();

                decodingBimapToBlur.setDecodingBimapToBlurCallback(new DecodingBimapToBlurCallback() {
                    @Override
                    public void onStart() {
                        extendlock=true;

                    }

                    @Override
                    public void onPost(Bitmap bitmap) {

                        Log.i("模糊回调","成功模糊化");
                      ;

                        backGroundBurnd.setImageBitmap(bitmap);
                        view.destroyDrawingCache();


                       presentSmallList();
                       presentBurdBg();
                        extendlock=false;
                        hideList();
                    }
                });

                decodingBimapToBlur.executeOnExecutor(ThreadExecutor.executorService,view.getDrawingCache());

            }


            //面板已展开,从左边转换到右边
            else if (smallOreation==oreation_left){

                smallOreation=oreation_right;

                hideSmallList();

                RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)smallSchemeList.getLayoutParams();
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);

                presentSmallList();

            }




        }

        //右缩小面板不展开

        else {

            if(isSmallPanelShowing){
              hideBurdBg();
              hideSmallList();
              presentList();
            }


        }

    }


    public void hideSmallList(){

        Animation animationout=new AlphaAnimation(1.0f,0f);
        animationout.setDuration(500);
        smallSchemeList.setVisibility(View.GONE);
        smallSchemeList.startAnimation(animationout);
        isSmallPanelShowing=false;

    }

    public void presentSmallList(){

        Animation animationin=new AlphaAnimation(0f,1.0f);
        animationin.setDuration(500);
        smallSchemeList.setVisibility(View.VISIBLE);
        smallSchemeList.startAnimation(animationin);
        isSmallPanelShowing=true;

    }

    public void hideBurdBg(){


        Animation animationout=new AlphaAnimation(1f,0f);
        animationout.setDuration(500);
        backGroundBurnd.setVisibility(View.GONE);
        backGroundBurnd.startAnimation(animationout);


    }

    public void presentBurdBg(){

        Animation animationin=new AlphaAnimation(0f,1.0f);
        animationin.setDuration(500);
        backGroundBurnd.setVisibility(View.VISIBLE);
        backGroundBurnd.startAnimation(animationin);

    }

    public void hideList(){

        Animation animationout=new AlphaAnimation(1f,0f);
        animationout.setDuration(500);
        schemeList.setVisibility(View.GONE);
        schemeList.startAnimation(animationout);

    }

    public void presentList(){

        Animation animationin=new AlphaAnimation(0f,1.0f);
        animationin.setDuration(500);
        schemeList.setVisibility(View.VISIBLE);
        schemeList.startAnimation(animationin);

    }



    public void disEnableDragGridViewMove(){
        if(schemeList != null) {
            schemeList.disEnableMove();
        }
        if(smallSchemeList != null) {
            smallSchemeList.disEnableMove();
        }
    }

    public void enableDragGridViewMove(){
        if(schemeList != null) {
            schemeList.enableMove();
        }
        if(smallSchemeList != null){
            smallSchemeList.enableMove();
        }
    }



}
